package com.qf.controller;

import com.qf.service.OrderService;
import com.qf.service.UserService;
import com.qf.util.ResponseUtil;
import com.qf.vo.*;
import org.omg.CORBA.PRIVATE_MEMBER;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.*;

@RestController
@CrossOrigin("*")
@RequestMapping("/stat")
public class StatController {
    @Autowired
    private UserService userService;
    
    @Autowired
    private OrderService orderService;

    @RequestMapping("/user")
    public Object user(){
        List<UserRowsVo> rows = userService.statisUsers();
        List<String> columns = new ArrayList<>();
        columns.add("day");
        columns.add("users");
        Map<String,Object> data = new HashMap<>();
        data.put("columns", columns);
        data.put("rows",rows);
        return ResponseUtil.ok(data);
    }

    @RequestMapping("/order")
    public Object order(){
        List<OrderRowsVo> orderStatList = orderService.findAll();
        List<Map<String,Object>> rows = new ArrayList<>();
        for (OrderRowsVo orderStat : orderStatList) {
            Map<String,Object> map = new HashMap<>();
            map.put("amount",orderStat.getAmount());
            map.put("orders",orderStat.getOrders());
            map.put("customers",orderStat.getCustomers());
            map.put("day",orderStat.getDay());
            map.put("pcr",orderStat.getPcr());
            rows.add(map);
        }
        Map<String,Object> data = new HashMap<>();
        List<String> columns = new ArrayList<>();
        columns.add("day");
        columns.add("orders");
        columns.add("customers");
        columns.add("amount");
        columns.add("pcr");
        data.put("columns", columns);
        data.put("rows",rows);
        return ResponseUtil.ok(data);
    }

    @RequestMapping("/goods")
    public Object goods(){
        List<String> columns = new ArrayList<>();
        columns.add("day");
        columns.add("orders");
        columns.add("products");
        columns.add("amount");
        List<GoodsRowsVo> goodsStatList = orderService.queryAllGoodsStat();
        List<Map<String,Object>> rows = new ArrayList<>();
        for (GoodsRowsVo goodsStat : goodsStatList) {
            Map<String,Object> map = new HashMap<>();
            map.put("amount",goodsStat.getAmount());
            map.put("orders",goodsStat.getOrders());
            map.put("day",goodsStat.getDay());
            map.put("products",goodsStat.getProducts());
            rows.add(map);
        }

        Map<String,Object> data = new HashMap<>();
        data.put("columns", columns);
        data.put("rows", rows);

        return ResponseUtil.ok(data);
    }
}
